/**
 * Created by freethy on 2017/10/13.
 */

/*
* 产生guid
* */
function guid() {
    function S4() {
        return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    }
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

/*
* 过滤json数组数据
* */
function filterJsonArray(data,field,value){
    var newData = [];
    for(var i=0;i<data.length;i++){
        if(data[i][field] == value){
            newData.push(data[i]);
        }
    }
    return newData;
}

function contains ( arr, needle ) {
    for (var i in arr) {
        if (arr[i] == needle) return true;
    }
    return false;
}

//绑定下拉框
function bindSelect(filter,data,valueField,textField,defaultText){
    $(filter).empty();
    $(filter).append('<option value="">'+defaultText+'</option>');
    for(var i=0;i<data.length;i++){
        $(filter).append('<option value="'+data[i][valueField]+'">'+data[i][textField]+'</option>');
    }
}


/*
 获取请求参数
 */
var urlParam = function (name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results) {
        return results[1];
    } else {
        return "";
    }
};


//扩充函数获取名字的方法
Function.prototype.getName = function(){
    return this.name || this.toString().match(/function\s*([^(]*)\(/)[1]
}

/*
*
* function js
*
* */
;(function (window) {

    /*
     获取当前页面所在的父页面iframe
     */
    function getCurrentIFrame() {
        var ifrs = parent.document.getElementsByTagName('iframe');
        for (var i = 0, j = ifrs.length; i < j; i++) {
            if (ifrs[i].contentWindow == window) {
                return ifrs[i];
            }
        }
    }


    //转换成float
    var toFloat = function(val){
        if(isNaN(Number(val)) || isNaN(parseFloat(val))) {
            val = null;
        } else {
            val = parseFloat(val);
        }
        return val;
    }



    //文件大小格式化
    var fileSize = function(size){
        if(size > 1024 * 1024 * 1024){
            return Math.round(size / 1024 / 1024 / 1024) + " GB";
        }
        if(size > 1024 * 1024){
            return Math.round(size / 1024 / 1024) + " MB";
        }
        if(size > 1024){
            return Math.round(size / 1024) + " KB";
        }
        return size + " B"
    }

    /*
    * 产生guid
    * */
    function guid() {
        function S4() {
            return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
        }
        return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
    }

    /*
     获取请求参数
     */
    var urlParam = function (name) {
        var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
        if (results) {
            return decodeURI(results[1]);
        } else {
            return "";
        }
    };

    window.func = {
        toFloat:toFloat,
        fileSize:fileSize,
        guid:guid,
        urlParam:urlParam,
        getCurrentIFrame:getCurrentIFrame,
    }
})(window);